from alibabacloud_chatbot20171011.client import Client as Chatbot20171011Client
from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models
from django.conf import settings


class AliClient(Chatbot20171011Client):
    def __init__(self):
        config = open_api_models.Config(
            # AccessKey ID,
            access_key_id=settings.ACCESS_KEY_ID,
            # AccessKey Secret,
            access_key_secret=settings.ACCESS_KEY_SECRET
        )
        config.endpoint = settings.ENDPOINT
        super(AliClient, self).__init__(config)


def ding_client(client):
    config = open_api_models.Config()
    config.protocol = 'https'
    config.region_id = 'central'
    return client(config)


def ding_ding_token() -> str:
    client = ding_client(dingtalkoauth2_1_0Client)
    get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest(
        app_key=settings.APP_KEY,
        app_secret=settings.APP_SECRET
    )
    response = client.get_access_token(get_access_token_request)
    return response.body.access_token
